<?php
// 本类由系统自动生成，仅供测试用途
class ArtcAction extends BaseAction {
	public function index() {
		
		$dao = D ( "Artc" );
		
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = $_REQUEST ['_order'];
		} else {
			$order = ! empty ( $sortBy ) ? $sortBy : $dao->getPk ();
		}
		//排序方式默认按照倒序排列
		//接受 sost参数 0 表示倒序 非0都 表示正序
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		
		//过滤条件
		$sort_id = ( int ) $_REQUEST ['sort_id'];
		$map ['pid'] = $sort_id;
		$this->assign ( 'sort_id', $sort_id );
		
		if (isset ( $_REQUEST ['title'] ) && ! empty ( $_REQUEST ['title'] )) {
			$map ['title'] = array ('like', $_REQUEST ['title'] . '%' );
			$this->assign ( 'title', $_REQUEST ['title'] );
		}
		
		if (isset ( $_REQUEST ['author'] ) && ! empty ( $_REQUEST ['author'] )) {
			$map ['author'] = $_REQUEST ['author'];
			$this->assign ( 'author', $map ['author'] );
		}
		
		if (isset ( $_REQUEST ['date'] ) && ! empty ( $_REQUEST ['date'] )) {
			$map ['date'] = $_REQUEST ['date'];
			$this->assign ( 'date', $map ['date'] );
		}
		
		//取得满足条件的记录数
		$count = $dao->where ( $map )
			->count ( 'id' );
		if ($count > 0) {
			import ( "ORG.Util.Page" );
			//创建分页对象
			if (! empty ( $_REQUEST ['listRows'] )) {
				$listRows = $_REQUEST ['listRows'];
			} else {
				$listRows = '';
			}
			$p = new Page ( $count, $listRows );
			//分页查询数据
			

			$voList = $dao->where ( $map )
				->order ( "`" . $order . "` " . $sort )
				->limit ( $p->firstRow . ',' . $p->listRows )
				->findAll ();
			//echo $model->getlastsql();
			//分页跳转的时候保证查询条件
			foreach ( $map as $key => $val ) {
				if (! is_array ( $val )) {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}
			//分页显示
			$page = $p->show ();
			//列表排序显示
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
			//模板赋值显示
			$this->assign ( 'list', $voList );
			$this->assign ( 'sort', $sort );
			$this->assign ( 'order', $order );
			$this->assign ( 'sortImg', $sortImg );
			$this->assign ( 'sortType', $sortAlt );
			$this->assign ( "page", $page );
		}
		
		$this->assign ( 'img_dir', C ( 'ARTC_IMG_DIR' ) . 's_' );
		Cookie::set ( '_currentUrl_', __SELF__ );
		
		$this->display ();
	}
	
	public function list_sort() {
		
		$id = ( int ) $_REQUEST ['id'];
		
		$dao = D ( "Sort" );
		$volist = $dao->order ( 'id asc' )
			->findAll ();
		
		$this->assign ( 'id', $id );
		$this->assign ( "list", $volist );
		$this->display ();
	}
	
	function _before_AddOrEdit() {
		$dao = D ( "Tpl" );
		
		$content_tpl = $dao->order ( 'is_default desc' )
			->where ( "tpl_type='content' and model='Artc'" )
			->findAll ();
		
		$this->assign ( 'content_tpl', $content_tpl );
	
	}
	
	function _before_edit() {
		$this->_before_AddOrEdit ();
		$this->assign ( 'img_dir', C ( 'ARTC_IMG_DIR' ) . 's_' );
	
	}
	
	function _before_update() {
		$dao = D ( "WebInfo" );
		$r = $dao->getField ( "artc_check" );
		if ($r ['artc_check'] == 1)
			$_POST ['checked'] = 0;
		else
			$_POST ['checked'] = 1;
			
		if( !isset( $_REQUEST['top'] ) )
			$_POST['top'] = 0;
	
	}
	
	function _before_insert() {
		$dao = D ( "WebInfo" );
		$r = $dao->getField ( "artc_check" );
		if ($r ['artc_check'] == 1)
			$_POST ['checked'] = 0;
		else
			$_POST ['checked'] = 1;
		$_POST['is_show'] = 1;
	}
	
	function _before_add() {
		$sort_id = ( int ) $_REQUEST ['sort_id'];
		$this->_before_AddOrEdit ();
		$this->assign ( 'date', date ( 'Y-m-d', time () ) );
		$this->assign ( 'pid', $sort_id );
	}
	
	public function _refresh_left($id) {
		echo "<script language=\"javascript\">parent.left.window.location.href='" . __URL__ . "/left/id/{$id}';</script>";
	
	}
	
	function moveTo() {
		$arr_id = $_REQUEST ['id'];
		if (empty ( $arr_id ) || ! is_array ( $arr_id )) {
			$this->error ( '非法操作！' );
		}
		//检查pid
		$pid = ( int ) $_REQUEST ['pid'];
		if (empty ( $pid ))
			$this->error ( '非法操作！' );
		
		$dao = D ( 'Artc' );
		$condition = array ('id' => array ('in', join ( ',', $arr_id ) ) );
		
		$rt = $dao->where ( $condition )
			->setField ( 'pid', $pid );
		if ($rt !== FALSE) {
			//$this->_refresh_left($pid);
			$this->assign ( 'jumpUrl', __URL__ . '/index/sort_id/' . $pid );
			$this->success ( '操作成功！' );
		} else {
			$this->error ( '移动文章失败！' );
		}
	
	}
	
	function update() {
		
		$dao = D ( 'Artc' );
		
		//处理上传的文章形象图片
		if (! empty ( $_FILES ['image'] ['size'] )) {
			
			$img = $this->_upload ();
			
			if ($img != null) {
				$_POST ['img'] = $img;
			}
		}
		
		//摘要
		if (empty ( $_REQUEST ['small_content'] )) {
			$_POST ['small_content'] = $this->get_small_content ( $_POST ['content'] );
		}
		
		//置顶
		if (empty ( $_POST ['top'] )) {
			$_POST ['top'] = 0;
		}
		
		//得到文章信息
		$id = ( int ) $_POST ['id'];
		$vo = $dao->where ( 'id=' . $id )
			->find ();
		
		// 更新数据
		if (false === $dao->create ()) {
			$this->error ( $dao->getError () );
		}
		$list = $dao->save ();
		if (false !== $list) {
			
			if ($img != null) {
				//删除文章原图
				$this->_del_img ( $vo ['img'] );
			}
			
			//成功提示
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ( '编辑成功!' );
		} else {
			//错误提示
			$this->error ( '编辑失败!' );
		}
	}
	
	//删除文章图片
	private function _del_img($img) {
		if (empty ( $img )) //如果没有图片,则直接返回
			return true;
		
		$img_path = C ( 'ARTC_IMG_UPLOAD_DIR' );
		unlink ( $img_path . $img ); //删除原图
		unlink ( $img_path . 's_' . $img ); //删除缩略图
		

		return true;
	
	}
	
	public function insert() {
		$dao = D ( 'Artc' );
		
		//处理上传的文章形象图片
		if (! empty ( $_FILES ['image'] ['size'] )) {
			
			$img = $this->_upload ();
			
			if ($img != null) {
				$_POST ['img'] = $img;
			}
		}
		
		//摘要
		if (empty ( $_REQUEST ['small_content'] )) {
			
			$_POST ['small_content'] = $this->get_small_content ( $_POST ['content'] );
		}
		
		//置顶
		if (empty ( $_POST ['top'] )) {
			$_POST ['top'] = 0;
		}
		
		if (false === $dao->create ()) {
			$this->error ( $dao->getError () );
		}
		
		//保存当前数据对象
		$list = $dao->add ();
		if ($list !== false) { //保存成功
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ( '新增成功!' );
		} else {
			//失败提示
			$this->error ( '新增失败!' );
		
		//$this->error( $model->getError () );
		}
	}
	
	private function get_small_content($content, $strlen = 100) {
		return msubstr ( strip_tags ( $content ), 0, $strlen );
	}
	
	/**
	 * 设置或取消置顶
	 *
	 */
	public function setTop() {
		$arr_id = $_REQUEST ['id'];
		if (empty ( $arr_id ) || ! is_array ( $arr_id )) {
			$this->error ( '非法操作！' );
		}
		
		//检查pid
		$pid = ( int ) $_REQUEST ['sort_id'];
		if (empty ( $pid ))
			$this->error ( '非法操作！' );
		
		$dao = D ( 'Artc' );
		$condition = array ('id' => array ('in', join ( ',', $arr_id ) ) );
		$bSet = ( int ) $_REQUEST ['bSet'];
		
		$rt = $dao->where ( $condition )
			->setField ( 'top', $bSet );
		if ($rt !== FALSE) {
			//$this->_refresh_left($pid);
			$this->assign ( 'jumpUrl', __URL__ . '/index/sort_id/' . $pid );
			$this->success ( '操作成功！' );
		} else {
			$this->error ( '置顶文章失败！' );
		}
	
	}
	
	/**
	 * 删除文章
	 */
	public function del() {
		$ids = $_REQUEST ['id'];
		if (empty ( $ids ))
			$this->error ( '参数Id:不能为空！' );
		if (is_array ( $ids )) {
			foreach ( $ids as $id ) {
				if (! $this->del_one ( $id )) {
					$this->error ( '删除失败(Id:' . $id . ')！' );
				}
			
			}
			$this->success ( '删除成功！' );
		} else {
			$id = ( int ) $ids;
			if (! $this->del_one ( $id )) {
				$this->error ( '删除失败(Id:' . $id . ')！' );
			}
		}
	
	}
	
	/**
	 * 删除一篇文章
	 */
	private function del_one($id) {
		$dao = D ( 'Artc' );
		if (! empty ( $dao )) {
			$vo = $dao->where ( "id=$id" )
				->find ();
			$rt = $dao->where ( "id=$id" )
				->delete ();
			if ($rt !== FALSE) {
				//删除文章图片
				$this->_del_img ( $vo ['img'] );
				
				return true;
			} else
				return false;
		} else
			return false;
	}
	
	protected function _upload() {
		import ( "ORG.Net.UploadFile" );
		$upload = new UploadFile ();
		//设置上传文件大小
		$upload->maxSize = 1024 * 1024;
		//设置上传文件类型
		$upload->allowExts = explode ( ',', 'jpg,gif,png,jpeg' );
		//设置附件上传目录
		$upload->savePath = C ( 'ARTC_IMG_UPLOAD_DIR' );
		//设置需要生成缩略图，仅对图像文件有效
		$upload->thumb = true;
		//设置需要生成缩略图的文件后缀
		$upload->thumbPrefix = 's_'; //生产2张缩略图
		//设置缩略图最大宽度
		$upload->thumbMaxWidth = C ( 'THUMB_W' );
		//设置缩略图最大高度
		$upload->thumbMaxHeight = C ( 'THUMB_H' );
		//设置上传文件规则
		$upload->saveRule = uniqid;
		//删除原图
		//$upload->thumbRemoveOrigin = true;
		if (! $upload->upload ()) {
			//捕获上传异常
			$this->error ( $upload->getErrorMsg () );
			return null;
		} else {
			//取得成功上传的文件信息
			$uploadList = $upload->getUploadFileInfo ();
			//$_POST['image']  = $uploadList[0]['savename'];
			return $uploadList [0] ['savename'];
		}
	}
	
	/**
	 * 保存排序结果
	 *
	 */
	
	public function save_sort() {
		$ids = $_REQUEST ['id'];
		
		if (empty ( $ids ) || ! is_array ( $ids )) {
			echo 0;
			exit ();
		}
		
		$dao = D ( 'Artc' );
		$sum = count ( $ids );
		for($i = 0; $i < $sum; $i ++) {
			$id = $ids [$i];
			$s = $_REQUEST ['sort'] [$i];
			$dao->where ( 'id=' . $id )
				->setField ( 'sort', $s );
		}
		echo 1;
	
	}
	
	public function setShow() {
		$show= (int)$_REQUEST['show'];
		$id = (int) $_REQUEST['id'];
		
		
		if( !empty($id) )
		{
			$dao = D('Artc');
			$dao->where('id='.$id)->setField('is_show',$show);
			$this->success ( '操作成功！');
		}
		else {
			$this->error('参数错误!');
		}
	}

}
?>